package org.example.socketdemos;

import org.example.utils.LogUtils;

import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;

/**
 * 老的socket不能解决粘包问题
 */
public class OldServerSocket {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket();

            serverSocket.bind(new InetSocketAddress("192.168.2.105", 8081));

            LogUtils.info("服务端启动");

            // 阻塞等待连接进来
            Socket socket = serverSocket.accept();

            LogUtils.info("有新的连接进来");

            if (socket != null) {
                InputStream ins = socket.getInputStream();

                int len = -1;
                byte[] contents = new byte[1024];
                while ((len = ins.read(contents)) > 0) {
                    System.out.println("获取客户端消息：" + new String(contents));
                }

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
